﻿using eCollabro.Client.Models.Core;
using eCollabro.Exceptions;
using eCollabro.Service.DataContracts;
using eCollabro.Service.ServiceContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace eCollabro.Client.ServiceProxy
{
    public class BaseServiceProxy
    {
        public RequestContextParameter RequestContext { get; set; }
        public ResponseContextParameter ResponseContext { get; set; }

        UserContextModel _userContext;

        public BaseServiceProxy()
        {
            this.RequestContext = new RequestContextParameter();
            this.ResponseContext = new ResponseContextParameter();
        }


        public void ChangeUserContextSite(int siteId)
        {
            this._userContext.SiteId = siteId;
            eCollabroRequest.ActiveUser = SecurityClientTranslate.Convert(_userContext);
        }

        public UserContextModel GetUserContext()
        {

            if (_userContext == null)
            {
                _userContext = new UserContextModel();
                if (System.Configuration.ConfigurationManager.AppSettings["SiteId"] != null)
                    this._userContext.SiteId = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SiteId"]);
                _userContext.Language = Thread.CurrentThread.CurrentUICulture.Name;
                _userContext.UserName = Thread.CurrentPrincipal.Identity.Name;
            }
            return _userContext;

        }


        /// <summary>
        /// HandleError
        /// </summary>
        /// <param name="ex"></param>
        protected bool HandleError(Exception ex)
        {
            bool handled = true;
            // add exceptions for which error need not to throw
            if (ex.GetType() != typeof(BusinessException))
            {
                // log error 
                //ExceptionLogger.LogError(ex);

            }
            handled = false;
            return handled;
        }


        /// <summary>
        /// HandleError
        /// </summary>
        /// <param name="responseStatus"></param>
        protected void HandleError(ResponseStatus responseStatus, ServiceResponseMessage responseMessage)
        {

            if (responseStatus == ResponseStatus.BusinessException)
            {
                throw new BusinessException(responseMessage.Message, responseMessage.MessageCode, responseMessage.Overridable);
            }

            else if (responseStatus == ResponseStatus.Exception)
            {
                throw new SysException(responseMessage.Message);
            }
        }

        /// <summary>
        /// SetRequestParameter
        /// </summary>
        /// <param name="serviceRequest"></param>
        protected void SetContext()
        {
            //Prometric.Proctoring.Service.se
            //serviceRequest.UserContext = Mapper.Map<UserContextModel, UserContextDC>(this.UserContext);
            //serviceRequest.RequestContextParameters=Mapper.Map<RequestContextParameter,RequestContextParameterDC>(this.RequestContext);
        }
    }
}
